package com.holyn.guofang.view;

import android.content.pm.ActivityInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.apkfuns.logutils.LogUtils;
import com.gyf.barlibrary.ImmersionBar;
import com.holyn.guofang.R;
import com.holyn.guofang.viewcommon.dialog.ToastDialog;
import com.holyn.mylibrary.LoadingDialog;
import com.vondear.rxtools.RxActivityUtils;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Holyn on 2017/5/17.
 */

public abstract class BaseActivity  extends AppCompatActivity {
    private View rootView;
    private Toolbar toolbar;
    private LinearLayout llBack;
    private ImageView ivBack;
    private TextView tvTitle;
    private TextView tvMenuRight;


    private LoadingDialog loadingDialog;
    private ToastDialog toastDialog;

    private int countNum = 18;
    private Disposable countLoadingDisposable = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // 禁止所有的activity横屏

        RxActivityUtils.addActivity(this);

        rootView = LayoutInflater.from(BaseActivity.this).inflate(getLayoutResID(), null);
        toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
        llBack = (LinearLayout) rootView.findViewById(R.id.ll_back);
        ivBack = (ImageView) rootView.findViewById(R.id.iv_back);
        tvTitle = (TextView) rootView.findViewById(R.id.tv_title);
        tvMenuRight = (TextView) rootView.findViewById(R.id.tv_menu_right);

        setContentView(rootView);

        ImmersionBar.with(this).statusBarDarkFont(true).init();

        onMyCreate();
    }

    @Override
    protected void onDestroy() {
        RxActivityUtils.removeActivity(this);
        super.onDestroy();
        ImmersionBar.with(this).destroy();  //不调用该方法，如果界面bar发生改变，在不关闭app的情况下，退出此界面再进入将记忆最后一次bar改变的状态
    }

    protected abstract int getLayoutResID();
    protected abstract void onMyCreate();

    /**
     * 在继承者的布局里面必须：<include layout="@layout/toolbar_custom"/>
     * @param title
     */
    protected void showToolbar(String title){
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        llBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onTopLeftBackPressListener(v);
            }
        });

        tvTitle.setText(title);
    }

    /**
     * 在继承者的布局里面必须：<include layout="@layout/toolbar_custom"/>
     * @param title
     * @param isShowBack
     */
    protected void showToolbar(String title, boolean isShowBack){
        if (isShowBack){
            showToolbar(title);
        } else {
            setSupportActionBar(toolbar);
            getSupportActionBar().setDisplayShowTitleEnabled(false);
            llBack.setClickable(false);
            ivBack.setVisibility(View.GONE);
            tvTitle.setText(title);
        }
    }

    protected void onTopLeftBackPressListener(View v){
        BaseActivity.this.finish();
    }

    protected void showToolbarMenuRight(String title, boolean isShowBack, String menu, View.OnClickListener onClickListener){
        showToolbar(title, isShowBack);
        tvMenuRight.setText(menu);
        tvMenuRight.setVisibility(View.VISIBLE);
        //添加文本时，设置MenuRight图片为空
        tvMenuRight.setCompoundDrawables(null,null,null,null);
        if (onClickListener != null){
            tvMenuRight.setOnClickListener(onClickListener);
        }
    }

    protected void showToolbarMenuRight(String title, boolean isShowBack, @DrawableRes int id, View.OnClickListener onClickListener){
        showToolbar(title, isShowBack);
        //设置右上角的图标
        Drawable leftDrawable = ContextCompat.getDrawable(BaseActivity.this, id);
        leftDrawable.setBounds(0, 0, leftDrawable.getMinimumWidth(), leftDrawable.getMinimumHeight());
        tvMenuRight.setCompoundDrawables(leftDrawable, null, null, null);
        tvMenuRight.setVisibility(View.VISIBLE);
        //添加图标时，设置MenuRight文本为空
        tvMenuRight.setText(null);

        if (onClickListener != null){
            tvMenuRight.setOnClickListener(onClickListener);
        }
    }

    protected void hideToolbarMenuRight(){
        tvMenuRight.setVisibility(View.GONE);
    }

    protected void showToolbarMenuRight(){
        tvMenuRight.setVisibility(View.VISIBLE);
    }

    protected void showLoadingDialog(){
        if (loadingDialog == null){
            loadingDialog = new LoadingDialog(BaseActivity.this);
        }
        loadingDialog.show();
    }

    protected void showLoadingDialog(String content){
        if (loadingDialog == null){
            loadingDialog = new LoadingDialog(BaseActivity.this);
        }
        loadingDialog.setContent(content);
        loadingDialog.show();
    }

    protected void closeLoadingDialog(){
        if (loadingDialog != null){
            if (loadingDialog.isShowing()){
                loadingDialog.dismiss();
            }
        }
    }

    protected void showToastDialog(String content){
        if (toastDialog == null){
            toastDialog = new ToastDialog(BaseActivity.this);
        }
        toastDialog.setTvContent(content);
        toastDialog.show();
    }

    protected void closeToastDialog(){
        if (toastDialog != null){
            if (toastDialog.isShowing()){
                toastDialog.dismiss();
            }
        }
    }

    protected void toastMsg(String text){
        Toast.makeText(BaseActivity.this, text, Toast.LENGTH_SHORT).show();
    }
    //Toast的长时间显示
    protected void toastMsg(String text,int cnt){
        Toast.makeText(BaseActivity.this, text, Toast.LENGTH_LONG).show();

    }

    /**
     *  展示加载的dialog，dialog的文字提示是不断增加的百分比
     */
    protected void showCountLoadingDialog(){
        if (loadingDialog == null){
            loadingDialog = new LoadingDialog(BaseActivity.this);
        }
        if (!loadingDialog.isShowing()){
            loadingDialog.show();
            loadingDialog.setContent("加载中18%...");
            startInterval();
        }
    }

    protected void closeCountLoadingDialog(){
        if (countLoadingDisposable != null){
            countLoadingDisposable.dispose();
        }
        if (loadingDialog != null){
            if (loadingDialog.isShowing()){
                loadingDialog.setContent("加载中100%...");
                countNum = 18;
                loadingDialog.dismiss();
            }
        }
    }

    /**
     *  启动百分比dialog的计时
     */
    protected void startInterval(){
        Observable.interval(0, 1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(@NonNull Disposable disposable) throws Exception {
                        countLoadingDisposable = disposable;
                    }
                }).subscribe(new Consumer<Long>() {
            @Override
            public void accept(@NonNull Long aLong) throws Exception {
                if (countNum < 99){
                    countNum++;
                    if (countNum%2 == 0) {
                        loadingDialog.setContent("加载中"+String.valueOf(countNum)+"%...");
                    } else {
                        loadingDialog.setContent("加载中"+String.valueOf(countNum)+"%....");
                    }
                }
            }
        });
    }
}
